package id.co.gomobile.assessment

class AssignmentController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST", selfAssign: "POST"]

    def assessmentService

    def index = {
        
    }

    def create = {
        def assignmentInstance = new Assignment()
        assignmentInstance.properties = params
        return [assignmentInstance: assignmentInstance]
    }

    def save = {
        def assignmentInstance = new Assignment(params)
        if (assessmentService.createAssignment(assignmentInstance)) {
			_sendAssignmentEmail(assignmentInstance)
			
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'assignment.label', default: 'Assignment'), assignmentInstance.id])}"
            redirect(action: "show", id: assignmentInstance.id)
        }
        else {
            render(view: "create", model: [assignmentInstance: assignmentInstance])
        }
    }

    def show = {
        def assignmentInstance = Assignment.get(params.id)
        if (!assignmentInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'assignment.label', default: 'Assignment'), params.id])}"
            redirect(action: "index")
        }
        else {
			def assigmentCategoryPoint = assessmentService.getAssignmentCategoryPoint(assignmentInstance)
            [assignmentInstance: assignmentInstance, assigmentCategoryPoint: assigmentCategoryPoint]
        }
    }

    def selfAssign = {
        def assignmentInstance = new Assignment(params)
        if (assessmentService.createAssignment(assignmentInstance)) {
			//_sendAssignmentEmail(assignmentInstance)
			
            flash.message = "${message(code: 'default.selfassigned.message', default: 'A test has been successfully assigned to you.')}"
			redirect(uri: "/")
        }
        else {
			redirect(uri: "/")
        }
    }
    
    def ofTest = {
    	def testInstance = Test.get(params.id)
        if (!testInstance) {
			params.finishedOnly = true // default form value			
			
			flash.message = "Please select a test"
		}
		else {
			params.max = Math.min(params.max ? params.int('max') : 10, 100)

			if(!params.sort) {
				params.sort = 'total'
				params.order = 'desc'
			}
			
			def assignmentInstanceList
			if(params.finishedOnly) {
				assignmentInstanceList = Assignment.findAllByTestAndEndTimeIsNotNull(testInstance, params)
			} else {
				assignmentInstanceList = Assignment.findAllByTest(testInstance, params)
			}
			
			[assignmentInstanceList: assignmentInstanceList, assignmentInstanceTotal: Assignment.count(), testInstance: testInstance]
		}
    }

    def delete = {
        def assignmentInstance = Assignment.get(params.id)
        if (assignmentInstance) {
			def userId = assignmentInstance.user.id
            try {
                assignmentInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'assignment.label', default: 'Assignment'), params.id])}"
				redirect(controller: "user", action: "show", id: userId)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'assignment.label', default: 'Assignment'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'assignment.label', default: 'Assignment'), params.id])}"
            redirect(controller: "user", action: "show", id: assignmentInstance.user.id)
        }
    }
	
	
	def _sendAssignmentEmail = { assignmentInstance ->
	sendMail {
	 	to assignmentInstance.user.email
	 	subject "${message(code: 'email.assignment.subject')}"
	 	body (view: "/shared/emails/assignment", model: [assignmentInstance: assignmentInstance])
	}
}
	
}
